<template>
  <AMISRenderer :schema="schema" />
</template>

<script setup>
import AMISRenderer from '@/components/AMISRenderer.vue'

let source = {
  url: '/base/region/province',
  adaptor: function (payload, response, api, context) {
    console.log(payload);
    if(payload.data){
      for(let i=0;i<payload.data.length;i++){
        payload.data[i].defer = true;
      }
    }
    return {
        ...payload
    };
  }
}

let deferApi = {
  url: '/base/region/children',
  adaptor: function (payload, response, api, context) {
    if(payload.data){
      for(let i=0;i<payload.data.length;i++){
        payload.data[i].defer = true;
      }
    }
    return {
        ...payload
    };
  },
  data:{
    parentId: '${id}'
  }
}

const schema = {
  type: 'page',
  body:{
      type: 'crud',
      name: 'region-list',
      id:'region-list',
      autoFillHeight: true,
      syncLocation: false,
      api: '/base/region/list',
      footerToolbar: [
        'statistics',
        'switch-per-page',
        'pagination'
      ],
      autoGenerateFilter: {
        showBtnToolbar: false,
        columnsNum: 3
      },
      headerToolbar:[
        'bulkActions',
        {
          type: 'button',
          actionType: 'dialog',
          align: 'right',
          label: '新增',
          icon: 'fa fa-plus pull-left',
          primary: true,
          dialog: {
            title: '新增片区',
            closeOnEsc: true,
            showErrorMsg: false,
            body:{
              type: 'form',
              api:'post:/base/region/add',
              canAccessSuperData: false,
              messages:{
                validateFailed:''
              },
              body:[
                {
                  type: 'input-text',
                  name: 'regionName',
                  required: true,
                  maxLength: 50,
                  label: '片区名称',
                  validationErrors: {
                    isRequired: '请输入片区名称'
                  }
                },
                {
                  type: 'input-text',
                  name: 'regionCode',
                  required: true,
                  label: '片区编码',
                  maxLength: 20,
                  validationErrors: {
                    isRequired: '请输入片区编码'
                  }
                },
                {
                  type: 'button-group-select',
                  name: 'level',
                  label: '级别',
                  source: '/base/dict/options/region-level',
                  required: true,
                  validationErrors: {
                    isRequired: '请选择级别'
                  }
                },
                {
                  type: 'tree-select',
                  name: 'parentId',
                  label: '上级片区',
                  labelField: 'regionName',
                  valueField: 'id',
                  initiallyOpen: false,
                  unfoldedLevel: 0,
                  deferApi: deferApi,
                  source: source,
                },
              ]
            }
          }
        },
        {
          type: 'button',
          actionType: 'dialog',
          align: 'right',
          label: '预览',
          dialog:{
            title: false,
            closeOnEsc: true,
            actions: [
              {
                type: 'button',
                actionType: 'close',
                label: '关闭',
                level: 'primary'
              }
            ],
            body: {
              type: 'input-tree',
                name: 'regionCode',
                source: '/base/region/tree',
                label: false,
                labelField: 'regionName',
                valueField: 'regionCode',
                initiallyOpen: false,
                unfoldedLevel: 0,
                deferApi: deferApi,
                source: source,
            }
          }
        }
      ],
      columns: [
          {
            name: 'regionName',
            label: '片区名称',
            searchable: {
              type: 'input-text',
              name: 'regionName',
              label: '片区名称',
              clearable: true
            }
          },
          {
            name: 'regionCode',
            label: '片区编码',
            searchable: {
              type: 'input-text',
              name: 'regionCode',
              label: '片区编码',
              clearable: true
            }
          },
          {
              name: 'levelStr',
              label: '级别'
          },
          {
            type: 'operation',
            label: '操作',
            buttons: [
              {
                label: '修改',
                type: 'button',
                actionType: 'dialog',
                dialog: {
                  title: '修改片区',
                  closeOnEsc: true,
                  showErrorMsg: false,
                  body:{
                    type: 'form',
                    api:'post:/base/region/update',
                    messages:{
                      validateFailed:''
                    },
                    body:[
                      {
                        type: 'hidden',
                        name: 'id',
                      },
                      {
                        type: 'input-text',
                        name: 'regionName',
                        required: true,
                        label: '片区名称',
                        maxLength: 50,
                      },
                      {
                        type: 'input-text',
                        name: 'regionCode',
                        required: true,
                        label: '片区编码',
                        maxLength: 20,
                      },
                      {
                        type: 'button-group-select',
                        name: 'level',
                        label: '级别',
                        source: '/base/dict/options/region-level',
                        required: true,
                        validationErrors: {
                          isRequired: '请选择级别'
                        }
                      },
                      {
                        type: 'tree-select',
                        name: 'parentId',
                        label: '上级片区',
                        labelField: 'regionName',
                        valueField: 'id',
                        initiallyOpen: false,
                        unfoldedLevel: 0,
                        deferApi: deferApi,
                        source: source,
                      },
                    ]
                  }
                }
              },
              {
                label: '删除',
                type: 'button',
                level: 'danger',
                actionType:'ajax',
                confirmText: '确认要删除该记录？',
                api: {
                  url: '/base/region/delete?id=${id}',
                  method: 'post',
                  data:{}
                },
              },
            ]
          }
      ]
  }
}

</script>
